package com.detooa.detooa.utils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.http.HttpMethod;

import com.detooa.common.utils.StringUtils;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Request.Builder;

public class OkHttpUtils {

    /**
     * 请求
     * 
     * @param httpMethod
     * @param url
     * @param sMediaType
     * @param sBody
     * @param headers
     * @return
     * @throws IOException
     */
    public static Response send(HttpMethod httpMethod, String url, String sMediaType, String sBody, Map<String, String> headers) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(30, TimeUnit.SECONDS).build();
        MediaType mediaType = MediaType.parse(sMediaType);
        RequestBody body = null;

        // 防止异常
        if (StringUtils.isNotEmpty(sBody)) {
            body = RequestBody.create(mediaType, sBody);
        }
        Builder builder = new Request.Builder().url(url).method(httpMethod.name(), body);

        // 默认值
        builder.addHeader("accept", "*/*");
        builder.addHeader("connection", "Keep-Alive");
        builder.addHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

        getHeaders(builder, headers);

        Request request = builder.build();
        return client.newCall(request).execute();
    }

    public static Response get(String url, String sMediaType, String sBody, Map<String, String> headers) throws IOException {
        return send(HttpMethod.GET, url, sMediaType, sBody, headers);
    }

    public static Response post(String url, String sMediaType, String sBody, Map<String, String> headers) throws IOException {
        return send(HttpMethod.POST, url, sMediaType, sBody, headers);
    }

    public static void getHeaders(Builder builder, Map<String, String> headers) {
        if (null != builder) {
            // 设置请求头
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    builder.addHeader(entry.getKey(), entry.getValue());
                }
            }
        }
    }

    public static String getParam(String url, Map<String, String> headers) {

        OkHttpClient client = new OkHttpClient();

        Request.Builder requestBuilder = new Request.Builder().url(url).get(); // 使用 GET 请求

//        // 设置请求头
//        for (String[] header : headers) {
//            if (header.length == 2) {
//                requestBuilder.addHeader(header[0], header[1]);
//            }
//        }
        // 设置请求头
        getHeaders(requestBuilder, headers);

        // 构建请求对象
        Request request = requestBuilder.build();

        // 发送请求并处理响应
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                System.out.println("Response: " + responseBody);

                return responseBody;
            } else {
                System.out.println("Request failed. Code: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
